package com.maxlen.goods.controller;

import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.R;
import com.maxlen.goods.entity.AttrAttrgroupRelationEntity;
import com.maxlen.goods.entity.AttrEntity;
import com.maxlen.goods.entity.dto.AttrAttrgroupRelationInfoDTO;
import com.maxlen.goods.entity.dto.AttrNoInfoDTO;
import com.maxlen.goods.entity.vo.AttrAttrgroupRelationDelVO;
import com.maxlen.goods.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 属性&属性分组关联
 *
 * @author MaxLen
 * @email MaxLen@gmail.com
 * @date 2022-10-27 10:12:58
 */
@RestController
@RequestMapping("goods/attrattrgrouprelation")
public class AttrAttrgroupRelationController {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    // @RequiresPermissions("goods:attrattrgrouprelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrAttrgroupRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 获取指定分组关联的所有属性
     */
    @GetMapping("/info/{attrgroupId}")
    // @RequiresPermissions("goods:attrattrgrouprelation:info")
    public R info(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities = attrAttrgroupRelationService.getDetailById(attrgroupId);
        List<AttrAttrgroupRelationInfoDTO> infoDTOList = CopyUtils.copyList(attrEntities, AttrAttrgroupRelationInfoDTO.class);
        return R.ok().put("data", infoDTOList);
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     */
    @GetMapping("/noInfo/{attrgroupId}")
    // @RequiresPermissions("goods:attrattrgrouprelation:info")
    public R noInfo(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrAttrgroupRelationService.getNoDetailById(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("goods:attrattrgrouprelation:save")
    public R save(@RequestBody List<AttrAttrgroupRelationDelVO> attrAttrgroupRelationDelVO){
		attrAttrgroupRelationService.saveDetail(attrAttrgroupRelationDelVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    // @RequiresPermissions("goods:attrattrgrouprelation:update")
    public R update(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation){
		attrAttrgroupRelationService.updateById(attrAttrgroupRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    // @RequiresPermissions("goods:attrattrgrouprelation:delete")
    public R delete(@RequestBody List<AttrAttrgroupRelationDelVO> relationDelVOList){
		attrAttrgroupRelationService.removeDetail(relationDelVOList);
        return R.ok();
    }

}
